﻿using System.Linq;
using System.Web.Mvc;
using Omu.Awesome.Mvc;
using Omu.AwesomeDemo.Core.Service;
using Omu.AwesomeDemo.Infra.Builder;
using Omu.AwesomeDemo.Infra.Dto;
using Omu.AwesomeDemo.Core.Model;

namespace Omu.AwesomeDemo.WebUI.Controllers
{
    public class UserController : Cruder<User, UserInput>
    {
        public UserController(ICrudService<User> s, IBuilder<User, UserInput> v) : base(s, v)
        {
        }
        public virtual ActionResult Search(int page = 1, int ps = 5)
        {
            var src = s.GetPageable(page, ps).Page;
            var rows = this.RenderView("rows", src);

            return Json(new { rows, more = s.Count() > page * ps });
        }
    }

    public class FoobarController : Cruder<Foobar, FoobarInput>
    {
        public FoobarController(ICrudService<Foobar> s, IBuilder<Foobar, FoobarInput> v) : base(s, v)
        {
        }

        public ActionResult Search(string name, int? show, int page = 1, int ps = 5)
        {
            var src = s.GetAll().Where(o => 
                (o.Name != null && o.Name.ToLower().Contains(name.ToLower())) &&
                (o.Show.Id == show || show == null));

            var rows = this.RenderView("rows", src.Skip((page - 1) * ps).Take(ps));

            return Json(new { rows, more = src.Count() > page * ps });
        }
    }
}